<?php

namespace Liaosp\Category\Http\Controllers;

use App\Models\Category;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;

class CategoryController extends Controller
{
    use ModelForm;

    protected $header = '类型管理';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('类型列表');

            $content->row(function ($row) {

                $row->column(3, $this->treeView()->render());

                $row->column(9, function ($column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('categories'));
                    $form->text('name','类型名称');
                    $form->UEditor('description','类型描述信息');
                    $form->number('order','排序序号');
                    $form->image('image_url','分类图片');
                    $form->select('parent_id','父类名称')->options(Category::selectOptions());
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });



        });
    }


    protected function treeView()
    {
        return Category::tree(function (Tree $tree) {
            $tree->disableCreate();
            $tree->branch(function ($branch){
                $html  = $branch['name'];
                if(empty($branch['image_url'])){
                    $branch['image_url'] = 'images/184863_20191203105946359.png';
                }
                $url =config('admin.upload.host') . '/' . $branch['image_url'];
                $html.=' &nbsp;&nbsp;&nbsp;<img src="'.$url.'" width="30" style=" border-radius: 25px;">';
                return $html;
            });
            return $tree;
        });
    }


    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑类型');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加类型');

            $content->body($this->form());
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Category::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name','类型名称');
            $form->file('image_url','分类图片');
            $form->UEditor('description','类型描述信息');
            $form->number('order','排序序号');
            $form->select('parent_id','父类名称')->options(Category::selectOptions());
        });
    }


}
